/*
 * @Author: 李一番
 * @Date: 2021-10-12 09:18:01
 * @Last Modified by: 李一番
 * @Last Modified time: 2021-12-15 14:15:17
 */
/**
 * 表单校验规则
 * @param rule
 * @param value
 * @param callback
 */
// 校验用户名
export const validateUsername = (
  rule: any,
  value: any,
  callback: (arg0?: Error | undefined) => void
) => {
  if (!value) {
    callback(new Error('用户名不可为空'))
  } else {
    callback()
  }
}
// 校验密码
export const validatePassword = (
  rule: any,
  value: any,
  callback: (arg0?: Error | undefined) => void
) => {
  if (value.length < 6) {
    callback(new Error('不能小于6个字符'))
  } else {
    callback()
  }
}

// 是否是外部的链接地址
export const isExternal = (path: string) => /^(https?:|mailto:|tel:)/.test(path)

// 是否是数组
export const isArray = (arg: any) => {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}
// 有效的地址
export const isValidURL = (url: string) => {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}
